package com.venus.controller;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.venus.entity.User;
import com.venus.service.UserService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.venus.util.JwtToken;
import com.venus.util.Result;
import com.venus.util.Token;

@RestController
public class LoginController {
	LocalDateTime localDateTime = LocalDateTime.now();



	@Resource
	private JwtToken jwtToken;
	@Resource
	private UserService userService;

	// 登录，需要拦截器直接放行，返回Token


	/**
	 * 登录
	 * @param loginName
	 * @param loginPwd
	 * @return Result
	 */
	@RequestMapping("/login")
	public Object login(String loginName,String loginPwd) {
		Map<String, Object> datamap = new HashMap<String, Object>();
		//根据用户名查询用户
		User user = userService.getOne(new QueryWrapper<User>().eq("user_name",loginName));
		//判断用户是否存在
		if (user != null) {
			//判断是否被锁定
			if (user.getIsLockout().equals("0")) {//未被锁定
				//判断密码是否正确
				if (loginPwd.equals(user.getUserPassword())) {//正确
					// 根据要求生成token
					//token生命周期   60*60*1000
					String tokenId = JwtToken.sign(new Token(user.getUserId(), user.getUserName(), user.getUserPassword()), 60 * 60  * 1000);

					//清空错误次数,清空锁定时间，更新登录时间，
					user.setUserPsdWrong(0);
					user.setUserLockTime(null);
					user.setUserLastLoginTime(localDateTime);
					//根据id修改用户信息

					userService.update(user,new UpdateWrapper<User>().eq("user_id",user.getUserId()));

					//保存用户信息
					String userDate = user.getUserId() + "#@" + user.getUserName() + "#@"+ user.getUserImg() + "#@" + tokenId;

					datamap.put("uid",user.getUserId());
					datamap.put("uname",user.getUserName());
					datamap.put("uimg",user.getUserImg());
					datamap.put("token",tokenId);
					//返回数据
					return new Result(0, "登录成功！",datamap);


				} else {//错误
					//错误太多，锁定用户
					if (user.getUserPsdWrong()==2){
						user.setUserPsdWrong(user.getUserPsdWrong()+1);
						user.setIsLockout("1");
						user.setUserLockTime(localDateTime);
						userService.update(user,new UpdateWrapper<User>().eq("user_id",user.getUserId()));
						return new Result(4, "您已错误三次，用户被锁定，请联系管理员");
					}else{
						//密码错误次数不足
						//错误次数加1
						user.setUserPsdWrong(user.getUserPsdWrong()+1);
						//根据id修改密码错误次数
						userService.update(user,new UpdateWrapper<User>().eq("user_id",user.getUserId()));
						int i=3-user.getUserPsdWrong();
						return new Result(3, "密码错误，你只有"+i+"次机会了");
					}

				}//密码判断结束
			} else {//被锁定
				return new Result(2, "用户已被锁定，请联系管理员");
			}//锁定判断结束

		} else {
			return new Result(1, "用户不存在");
		}//用户名判断结束


	}

}
